package com.example.javabasic.other;

import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @description trie树
 * @Author Lin FuYuan
 * @date 2021/4/15
 */
public class Trie {

    private Node root = new Node("",0,1);

    public void insert(String[] url,String value){
        Node node = root;
        OUT:
        for (int i = 0; i < url.length; i++) {
            String s = url[i];
            List<Node> childs = node.getChilds();
            Node n = new Node(s.trim(),i+1,i==url.length-1?1:0);
            if(i == url.length-1){
                n.setValue(value);
            }
            if(CollectionUtils.isEmpty(childs)){
                childs.add(n);
                node = n;
                continue ;
            }
            for (Node child : childs) {
                if(child.getName().equals(s)){
                    node = child;
                    continue OUT;
                }
            }
            childs.add(n);
            node = n;
        }
    }

    public Node getNode(){
        return root;
    }


}
